﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

public class SettingsManager
{
    // INI文件路径（默认为应用程序目录下的Settings.ini）
    private string _iniPath;

    // Windows API 函数声明
    [DllImport("kernel32")]
    private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

    [DllImport("kernel32")]
    private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

    public SettingsManager(string iniPath = null)
    {
        _iniPath = iniPath ?? Path.Combine(Application.StartupPath, "Settings.ini");
    }

    // 写入INI文件
    public void WriteValue(string section, string key, string value)
    {
        WritePrivateProfileString(section, key, value, _iniPath);
    }

    // 读取INI文件
    public string ReadValue(string section, string key, string defaultValue = "")
    {
        var temp = new StringBuilder(255);
        GetPrivateProfileString(section, key, defaultValue, temp, 255, _iniPath);
        return temp.ToString();
    }
}
